package com.cn.esermis.excelReader;


import com.cn.esermis.coding.PollutionGas;
import com.cn.esermis.dpld.model.DataAllVo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;


public class ExcelReader {

    public List<Map<Integer,Object>> readExcelFile(String filePath) throws IOException {
        List<Map<Integer,Object>> data = new LinkedList<>();

        FileInputStream fis = new FileInputStream(new File(filePath));
        Workbook workbook = new HSSFWorkbook(fis);
        Sheet sheet = workbook.getSheetAt(0);

        Iterator<Row> rowIterator = sheet.iterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            if(row.getRowNum() == 0){
                continue;
            }
//            List<String> rowData = new ArrayList<>();
            Map<Integer,Object> map = new LinkedHashMap<>();
            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                // 行标
                int columnIndex = cell.getColumnIndex();
                switch (cell.getCellType()) {
                    case STRING:
                        map.put(columnIndex,cell.getStringCellValue());
                        break;
                    case NUMERIC:
                        map.put(columnIndex,String.valueOf(cell.getNumericCellValue()));
                        break;
                    case BOOLEAN:
                        map.put(columnIndex,String.valueOf(cell.getBooleanCellValue()));
                        break;
                    default:
                        map.put(columnIndex,"");
                        break;
                }
            }
            data.add(map);
        }

        workbook.close();
        fis.close();

        return data;
    }

    public List<DataAllVo> getData() {
        String filePath = "E:\\Desktop\\排口信息.xls";
        List<DataAllVo> dataAcquisitionInstrumentret = new ArrayList<>();
        ExcelReader excelReader = new ExcelReader();
        try {
            List<Map<Integer, Object>> maps = excelReader.readExcelFile(filePath);
            List<DataAllVo> dataAcquisitionInstruments = new ArrayList<>();

            maps.forEach(x->{
                DataAllVo instrumentVo = new DataAllVo();
//                instrumentVo.setAnalyticalInstruments();
//                instrumentVo.setPageNum();
//                instrumentVo.setPageSize();
//                instrumentVo.setCompanyName();
//                instrumentVo.setDepartmentName();
//                instrumentVo.setId();
                instrumentVo.setOutletName(String.valueOf(x.get(6)!=null?x.get(6):""));
                instrumentVo.setOutletCode(String.valueOf(x.get(7)!=null?x.get(7):""));
                instrumentVo.setType(String.valueOf(x.get(8)!=null?x.get(8):"").equals("废气")?1:2);
//                instrumentVo.setUseData();
                if (x.get(14)!=null
                        && !String.valueOf(x.get(14)).equals("是")){
                    instrumentVo.setCompleteFilings(1);
                }
//                instrumentVo.setCompleteFilingsTime();
                instrumentVo.setCollectionName(x.get(18)!=null? String.valueOf(x.get(18)) :"");
                instrumentVo.setCollectionBrand(x.get(18)!=null? String.valueOf(x.get(18)) :"");
                instrumentVo.setCollectionModel(x.get(19)!=null? String.valueOf(x.get(19)) :"");
                instrumentVo.setMnCode(x.get(17)!=null? String.valueOf(x.get(17)) :"");
//                if ()

//                instrumentVo.setDepartmentCode();
                instrumentVo.setHaveExhaust(String.valueOf(x.get(9)).equals("是")?1:0);
                instrumentVo.setExhaustTemperature(x.get(11)!=null? String.valueOf(x.get(11)) :"");
                instrumentVo.setExhaustHeight(x.get(10)!=null? String.valueOf(x.get(10)) :"");
                instrumentVo.setExhaustDiameter(x.get(12)!=null? String.valueOf(x.get(12)) :"");
                if (x.get(2)==null) {
                    DataAllVo instrumentVo1 = dataAcquisitionInstruments.get(0);
                    instrumentVo.setIndustryCategory(instrumentVo1.getIndustryCategory());
                    instrumentVo.setLicenseNumber(instrumentVo1.getLicenseNumber());
                    instrumentVo.setLicenseData(instrumentVo1.getLicenseData());
                    instrumentVo.setCompanyCode(instrumentVo1.getCompanyCode());
                }else if(x.get(2)!=null && String.valueOf(x.get(2)).equals("") ){
                    DataAllVo instrumentVo1 = dataAcquisitionInstruments.get(0);
                    instrumentVo.setIndustryCategory(instrumentVo1.getIndustryCategory());
                    instrumentVo.setLicenseNumber(instrumentVo1.getLicenseNumber());
                    instrumentVo.setLicenseData(instrumentVo1.getLicenseData());
                    instrumentVo.setCompanyCode(instrumentVo1.getCompanyCode());
                }
                else{
                    dataAcquisitionInstrumentret.addAll(dataAcquisitionInstruments);
                    dataAcquisitionInstruments.clear();
                    instrumentVo.setIndustryCategory(x.get(2)!=null? String.valueOf(x.get(2)) :"");
                    instrumentVo.setLicenseNumber(x.get(3)!=null? String.valueOf(x.get(3)) :"");
                    instrumentVo.setLicenseData(x.get(4)!=null? String.valueOf(x.get(4)) :"");
                    instrumentVo.setCompanyCode(x.get(1)!=null? String.valueOf(x.get(1)) :"");
                }
                // 分析仪
                instrumentVo.setBrand(x.get(21)!=null? String.valueOf(x.get(21)) :"");
                instrumentVo.setModel(x.get(22)!=null? String.valueOf(x.get(22)) :"");
                instrumentVo.setName(x.get(20)!=null? String.valueOf(x.get(20)) :"");
//                instrumentVo.setFactorInfor(x.get(22)!=null? String.valueOf(x.get(22)) :"");
//                instrumentVo.setRange(x.get(23)!=null? String.valueOf(x.get(23)) :"");
//                instrumentVo.setDataAcquisitionInstrumentId(x.get(24)!=null? String.valueOf(x.get(24)) :"");
                instrumentVo.setReferenceOxygenContent(x.get(23)!=null? String.valueOf(x.get(23)) :"");
                instrumentVo.setVelocityFieldCoefficient(x.get(24)!=null? String.valueOf(x.get(24)) :"");
                instrumentVo.setPitotTubeCoefficient(x.get(25)!=null? String.valueOf(x.get(25)) :"");
                instrumentVo.setKvalue(x.get(27)!=null? String.valueOf(x.get(27)) :"");
                instrumentVo.setFlueArea(x.get(26)!=null? String.valueOf(x.get(26)) :"");
                instrumentVo.setDilutionRatio(x.get(28)!=null? String.valueOf(x.get(28)) :"");



                instrumentVo.setHoursAvg(x.get(31)!=null? String.valueOf(x.get(31)) :"");
                instrumentVo.setHoursUnit(x.get(32)!=null? String.valueOf(x.get(32)) :null);
                instrumentVo.setMonthlyEmissions(x.get(33)!=null? String.valueOf(x.get(33)) :"");
                instrumentVo.setMonthlyEmissionsUnit(x.get(34)!=null? String.valueOf(x.get(34)) :null);
                instrumentVo.setYearEmissions(x.get(35)!=null? String.valueOf(x.get(35)) :"");
                instrumentVo.setYearEmissionsUnit(x.get(36)!=null? String.valueOf(x.get(36)) :null);
                instrumentVo.setRangeStr(x.get(37)!=null? String.valueOf(x.get(37)) :"");
                instrumentVo.setRangeUnit(x.get(38)!=null? String.valueOf(x.get(38)) :"");
                // 检测因子信息
                instrumentVo.setFactorName(x.get(30)!=null? String.valueOf(x.get(30)) :"");

                PollutionGas[] values = PollutionGas.values();
                for (PollutionGas value : values) {
                    if (value.mean().equals(instrumentVo.getFactorName())){
                        instrumentVo.setFactor(value.code());
                    }
                }

                dataAcquisitionInstruments.add(instrumentVo);

            });
//            System.out.println(dataAcquisitionInstrumentret);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataAcquisitionInstrumentret;
    }



    public static void main(String[] args) {
        String filePath = "E:\\Desktop\\排口信息.xls";

        ExcelReader excelReader = new ExcelReader();
        try {
            List<Map<Integer, Object>> maps = excelReader.readExcelFile(filePath);
            List<DataAllVo> dataAcquisitionInstruments = new ArrayList<>();
            List<DataAllVo> dataAcquisitionInstrumentret = new ArrayList<>();
            maps.forEach(x->{
                DataAllVo instrumentVo = new DataAllVo();
//                instrumentVo.setAnalyticalInstruments();
//                instrumentVo.setPageNum();
//                instrumentVo.setPageSize();
//                instrumentVo.setCompanyName();
//                instrumentVo.setDepartmentName();
//                instrumentVo.setId();
                instrumentVo.setOutletName(String.valueOf(x.get(6)!=null?x.get(6):""));
                instrumentVo.setOutletCode(String.valueOf(x.get(6)!=null?x.get(6):""));
                instrumentVo.setType(String.valueOf(x.get(6)!=null?x.get(6):"").equals("废气")?1:2);
//                instrumentVo.setUseData();
                if (x.get(14)!=null
                        && !String.valueOf(x.get(14)).equals("是")){
                    instrumentVo.setCompleteFilings(1);
                }
//                instrumentVo.setCompleteFilingsTime();
                instrumentVo.setCollectionName(x.get(18)!=null? String.valueOf(x.get(18)) :"");
                instrumentVo.setCollectionBrand(x.get(18)!=null? String.valueOf(x.get(18)) :"");
                instrumentVo.setCollectionModel(x.get(19)!=null? String.valueOf(x.get(19)) :"");
                instrumentVo.setMnCode(x.get(17)!=null? String.valueOf(x.get(17)) :"");
//                if ()

//                instrumentVo.setDepartmentCode();
                instrumentVo.setHaveExhaust(String.valueOf(x.get(9)).equals("是")?1:0);
                instrumentVo.setExhaustTemperature(x.get(11)!=null? String.valueOf(x.get(11)) :"");
                instrumentVo.setExhaustHeight(x.get(10)!=null? String.valueOf(x.get(10)) :"");
                instrumentVo.setExhaustDiameter(x.get(12)!=null? String.valueOf(x.get(12)) :"");
                if (x.get(2)==null) {
                    DataAllVo instrumentVo1 = dataAcquisitionInstruments.get(0);
                    instrumentVo.setIndustryCategory(instrumentVo1.getIndustryCategory());
                    instrumentVo.setLicenseNumber(instrumentVo1.getLicenseNumber());
                    instrumentVo.setLicenseData(instrumentVo1.getLicenseData());
                    instrumentVo.setCompanyCode(instrumentVo1.getCompanyCode());
                }else if(x.get(2)!=null && String.valueOf(x.get(2)).equals("") ){
                    DataAllVo instrumentVo1 = dataAcquisitionInstruments.get(0);
                    instrumentVo.setIndustryCategory(instrumentVo1.getIndustryCategory());
                    instrumentVo.setLicenseNumber(instrumentVo1.getLicenseNumber());
                    instrumentVo.setLicenseData(instrumentVo1.getLicenseData());
                    instrumentVo.setCompanyCode(instrumentVo1.getCompanyCode());
                }
                else{
                    dataAcquisitionInstrumentret.addAll(dataAcquisitionInstruments);
                    dataAcquisitionInstruments.clear();
                    instrumentVo.setIndustryCategory(x.get(2)!=null? String.valueOf(x.get(2)) :"");
                    instrumentVo.setLicenseNumber(x.get(3)!=null? String.valueOf(x.get(3)) :"");
                    instrumentVo.setLicenseData(x.get(4)!=null? String.valueOf(x.get(4)) :"");
                    instrumentVo.setCompanyCode(x.get(1)!=null? String.valueOf(x.get(1)) :"");
                }
                // 分析仪
                instrumentVo.setBrand(x.get(20)!=null? String.valueOf(x.get(20)) :"");
                instrumentVo.setModel(x.get(21)!=null? String.valueOf(x.get(21)) :"");
                instrumentVo.setName(x.get(20)!=null? String.valueOf(x.get(20)) :"");
//                instrumentVo.setFactorInfor(x.get(22)!=null? String.valueOf(x.get(22)) :"");
//                instrumentVo.setRange(x.get(23)!=null? String.valueOf(x.get(23)) :"");
//                instrumentVo.setDataAcquisitionInstrumentId(x.get(24)!=null? String.valueOf(x.get(24)) :"");
                instrumentVo.setReferenceOxygenContent(x.get(22)!=null? String.valueOf(x.get(22)) :"");
                instrumentVo.setVelocityFieldCoefficient(x.get(23)!=null? String.valueOf(x.get(23)) :"");
                instrumentVo.setPitotTubeCoefficient(x.get(24)!=null? String.valueOf(x.get(24)) :"");
                instrumentVo.setKvalue(x.get(26)!=null? String.valueOf(x.get(26)) :"");
                instrumentVo.setFlueArea(x.get(25)!=null? String.valueOf(x.get(25)) :"");
                instrumentVo.setDilutionRatio(x.get(27)!=null? String.valueOf(x.get(27)) :"");



                instrumentVo.setHoursAvg(x.get(31)!=null? String.valueOf(x.get(31)) :"");
                instrumentVo.setHoursUnit(x.get(32)!=null? String.valueOf(x.get(32)) :null);
                instrumentVo.setMonthlyEmissions(x.get(33)!=null? String.valueOf(x.get(33)) :"");
                instrumentVo.setMonthlyEmissionsUnit(x.get(34)!=null? String.valueOf(x.get(34)) :null);
                instrumentVo.setYearEmissions(x.get(35)!=null? String.valueOf(x.get(35)) :"");
                instrumentVo.setYearEmissionsUnit(x.get(36)!=null? String.valueOf(x.get(36)) :null);
                instrumentVo.setRangeStr(x.get(37)!=null? String.valueOf(x.get(37)) :"");
                instrumentVo.setRangeUnit(x.get(38)!=null? String.valueOf(x.get(38)) :"");
                // 检测因子信息
                instrumentVo.setFactorName(x.get(30)!=null? String.valueOf(x.get(30)) :"");

                PollutionGas[] values = PollutionGas.values();
                for (PollutionGas value : values) {
                    if (value.mean().equals(instrumentVo.getFactorName())){
                        instrumentVo.setFactor(value.code());
                    }
                }

                dataAcquisitionInstruments.add(instrumentVo);

            });
            System.out.println(dataAcquisitionInstrumentret);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
